package com.wangzeqi.changliaoba.login;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.wangzeqi.changliaba.dao.User;
import com.wangzeqi.changliaoba.R;
import com.wangzeqi.changliaoba.base.BaseActivity;
import com.wangzeqi.changliaoba.singleton.AllSingleTon;


/**
 * Created by dllo on 16/3/1.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private TextView textView;
    private EditText editTextP, editTextC, editTextCT;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_register);
        textView = (TextView) findViewById(R.id.cancel_tv);
        editTextP = (EditText) findViewById(R.id.register_name);
        editTextC = (EditText) findViewById(R.id.register_code_one);
        editTextCT = (EditText) findViewById(R.id.register_code_two);
        button = (Button) findViewById(R.id.next_btn);
        textView.setOnClickListener(this);
        button.setOnClickListener(this);
        button.setClickable(false);
        button.setTextColor(Color.GRAY);
        editTextCT.addTextChangedListener(new EditChangedListener());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel_tv:

                finish();
                break;
            case R.id.next_btn:
                register();
                break;
        }
    }

    public void register() {
        final String username = editTextP.getText().toString().trim();
        final String password = editTextC.getText().toString().trim();
        String confirm_password = editTextCT.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, getResources().getString(R.string.User_name_cannot_be_empty), Toast.LENGTH_SHORT).show();
            editTextP.requestFocus();
            return;
        } else if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, getResources().getString(R.string.Password_cannot_be_empty), Toast.LENGTH_SHORT).show();
            editTextC.requestFocus();
            return;
        } else if (TextUtils.isEmpty(confirm_password)) {
            Toast.makeText(this, getResources().getString(R.string.Confirm_password_cannot_be_empty), Toast.LENGTH_SHORT).show();
            editTextCT.requestFocus();
            return;
        } else if (!password.equals(confirm_password)) {
            Toast.makeText(this, getResources().getString(R.string.Two_input_password), Toast.LENGTH_SHORT).show();
            return;
        }
        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
            final ProgressDialog pd = new ProgressDialog(this);
            pd.setMessage("正在注册……");
            pd.show();

            new Thread(new Runnable() {
                public void run() {
                    try {
                        // 调用sdk注册方法
                        EMChatManager.getInstance().createAccountOnServer(username, password);
                        runOnUiThread(new Runnable() {
                            public void run() {
                                if (!RegisterActivity.this.isFinishing())
                                    pd.dismiss();

//                                AllSingleTon.getOurInstance().getDaoSession().getUserDao().insert(new User(AllSingleTon.getOurInstance().getDaoSession().getUserDao().count(),username,password));
                                // 保存用户名
//                                Helper.getInstance().setCurrentUserName(username);
                                Toast.makeText(RegisterActivity.this, "注册成功，请登录", Toast.LENGTH_SHORT).show();
                                finish();
                                Intent intent2 = new Intent(RegisterActivity.this,EnterActivity.class);
                                startActivity(intent2);
                            }
                        });
                    } catch (final EaseMobException e) {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                if (!RegisterActivity.this.isFinishing())
                                    pd.dismiss();
                                int errorCode = e.getErrorCode();
                                if (errorCode == EMError.NONETWORK_ERROR) {
                                    Toast.makeText(RegisterActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                                } else if (errorCode == EMError.USER_ALREADY_EXISTS) {
                                    Toast.makeText(RegisterActivity.this, "用户已存在", Toast.LENGTH_SHORT).show();
                                } else if (errorCode == EMError.UNAUTHORIZED) {
                                    Toast.makeText(RegisterActivity.this, "组册失败 没有权限", Toast.LENGTH_SHORT).show();
                                } else if (errorCode == EMError.ILLEGAL_USER_NAME) {
                                    Toast.makeText(RegisterActivity.this, "非法的用户名", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }
            }).start();

        }
    }

    class EditChangedListener implements TextWatcher {

        private CharSequence temp; // 监听前的文本

        // 输入文本之前的状态
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            temp = s;
        }

        // 输入文字中的状态，count是一次性输入字符数
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (6 - s.length() <= 0) {
                button.setClickable(true);
                button.setTextColor(Color.WHITE);
            }else {
                button.setClickable(false);
                button.setTextColor(Color.GRAY);
            }
        }
        // 输入文字后的状态
        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
